package com.coders.powergraph.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import com.coders.ChartActivity;
import com.coders.powergraph.PowerBean;

import android.graphics.Color;
import android.util.Log;

/**
 * @author Subin
 * 
 */
public class DataProvider
{
    public static List< PowerBean > getPowerBeans( ChartActivity chartActivity )
    {
        List< PowerBean > list = new LinkedList< PowerBean >();

        try
        {
            InputStream in = chartActivity.getAssets().open( "weekcost.txt" );
            BufferedReader br = new BufferedReader( new InputStreamReader( in ) );
            String strLine;
            StringTokenizer tokenizer;
            while ( ( strLine = br.readLine() ) != null )
            {
                tokenizer = new StringTokenizer( strLine, "," );

                PowerBean bean = new PowerBean();
                if ( tokenizer.hasMoreElements() )
                    bean.setX( new Float( tokenizer.nextElement().toString() ) );
                if ( tokenizer.hasMoreElements() )
                    bean.setY( new Float( tokenizer.nextElement().toString() ) );
                if ( tokenizer.hasMoreElements() )
                {
                    String color = tokenizer.nextElement().toString();
                    if ( "red".equalsIgnoreCase( color ) )
                    {
                        bean.setColorCode( Color.RED );
                    }
                    else if ( "green".equalsIgnoreCase( color ) )
                    {
                        bean.setColorCode( Color.GREEN );
                    }
                    else
                    {
                        bean.setColorCode( Color.YELLOW );
                    }
                }
                list.add( bean );
            }
            in.close();
        }
        catch ( Exception e )
        {
            Log.e( "DataProvider", e.getMessage(), e );
        }

        return list;

    }
}